package pi.client.utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import pi.client.board.MapStructure;

public class FileUtils {
	static public MapStructure LoadMap(String filename) {
		MapStructure map = new MapStructure();

		BufferedReader bufferedReader = null;
		try {
			//bufferedReader = new BufferedReader(new FileReader(filename));
			bufferedReader = new BufferedReader(new InputStreamReader(FileUtils.class.getClass().getResource("/"+filename).openStream()));
			String line = null;
			
			int lineCount = 1;
			while ((line = bufferedReader.readLine()) != null) {
				if(lineCount == 1){
					map.mapName = line;
				}else if(lineCount == 2){
					String split[];
					split = line.split(";");
					map.width = Integer.parseInt(split[0]);
					map.height = Integer.parseInt(split[1]);
					map.CreateData();
				}else{
					String split[];
					split = line.split(";");
					for(int i = 0; i < split.length; i++){
						map.data[i][lineCount-3] = Integer.parseInt(split[i]);
					}
				}
								
				lineCount++;
			}

		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		
		return map;
	}
}
